package com.acane.instagram.service.impl;

import com.acane.instagram.mapper.FollowMapper;
import com.acane.instagram.pojo.Result;
import com.acane.instagram.pojo.User;
import com.acane.instagram.pojo.dto.FollowDTO;
import com.acane.instagram.service.FollowService;
import com.acane.instagram.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, FollowDTO> implements FollowService {

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private UserService userService;

    @Override
    public Result getFollowHistory(String userId) {
        List<FollowDTO> followDTOList = followMapper.getFollowHistory(userId);
        return Result.success(followDTOList);
    }

    @Override
    public Result follow(String followingId, String followerId) {
        // 更新用户关注数量
        User follower = userService.getUserById(followerId);
        follower.setFollowingCount(follower.getFollowingCount() + 1);
        userService.updateById(follower);
        // 更新用户粉丝数量
        User following = userService.getUserById(followingId);
        following.setFollowerCount(following.getFollowerCount() + 1);
        userService.updateById(following);

        // 判断是否有数据
        FollowDTO databaseFollowDTO = followMapper.selectOne(new LambdaQueryWrapper<FollowDTO>()
                .eq(FollowDTO::getFollowingId, followingId)
                .eq(FollowDTO::getFollowerId, followerId));

        // 如果有，则改变状态即可
        if (databaseFollowDTO != null) {
            databaseFollowDTO.setStatus(1);
            followMapper.update(databaseFollowDTO, new LambdaQueryWrapper<FollowDTO>().
                    eq(FollowDTO::getId, databaseFollowDTO.getId()));
            return Result.success(databaseFollowDTO);
        }
        // 如果没有，则插入一条数据
        FollowDTO followDTO = new FollowDTO();
        followDTO.setId(UUID.randomUUID().toString().replace("-", ""));
        followDTO.setFollowingId(followingId);
        followDTO.setFollowerId(followerId);
        followDTO.setCreateTime(LocalDateTime.now().toString());
        followDTO.setUpdateTime(LocalDateTime.now().toString());
        followDTO.setStatus(1);
        followMapper.insert(followDTO);
        return Result.success(followDTO);
    }

    @Override
    public Result unFollow(String followingId, String followerId) {
        // 更新用户关注数量
        User follower = userService.getUserById(followerId);
        follower.setFollowingCount(follower.getFollowingCount() - 1);
        userService.updateById(follower);
        // 更新用户粉丝数量
        User following = userService.getUserById(followingId);
        following.setFollowerCount(following.getFollowerCount() - 1);
        userService.updateById(following);
        // 获取关注数据
        FollowDTO databaseFollowDTO = followMapper.selectOne(new LambdaQueryWrapper<FollowDTO>()
                .eq(FollowDTO::getFollowingId, followingId)
                .eq(FollowDTO::getFollowerId, followerId));
        databaseFollowDTO.setStatus(0);
        followMapper.update(databaseFollowDTO, new LambdaQueryWrapper<FollowDTO>().
                eq(FollowDTO::getId, databaseFollowDTO.getId()));
        System.out.println("修改后的：" + databaseFollowDTO);
        return Result.success(databaseFollowDTO);
    }

    @Override
    public Result getFollowerList(String userId) {
        List<User> followerList = followMapper.getFollowerList(userId);
        if (!followerList.isEmpty()) {
            return Result.success(followerList);
        }
        return Result.failure(404, "没有关注者");
    }

    @Override
    public Result getFollowingList(String userId) {
        List<User> followingList = followMapper.getFollowingList(userId);
        if (!followingList.isEmpty()) {
            return Result.success(followingList);
        }
        return Result.failure(404, "没有关注者");
    }
}
